<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>品牌管理</el-breadcrumb-item>
    </el-breadcrumb>

    <h3 style="margin: 20px 0;">品牌管理</h3>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="50"></el-table-column>
      <el-table-column prop="name" label="名称" width="80"></el-table-column>
      <el-table-column prop="pinyin" label="拼音" width="100"></el-table-column>
      <el-table-column prop="keywords" label="关键词" width="300"></el-table-column>
      <el-table-column prop="productCount" label="商品数量" width="80"></el-table-column>
      <el-table-column prop="sales" label="销量" width="80"></el-table-column>
      <el-table-column prop="positiveCommentCount" label="好评" width="80"></el-table-column>
      <el-table-column prop="commentCount" label="评论数量" width="80"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button
              size="mini"
              type="danger"
              @click="openDeleteConfirm(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: []
    }
  },
  methods: {
    openDeleteConfirm(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(id);
      }).catch(() => {
      });
    },
    handleEdit(id) {
      console.log('将编辑id=' + id + "的品牌数据……");
    },
    handleDelete(id) {
      console.log('将删除id=' + id + "的品牌数据……");
      let url = 'http://localhost:9080/brands/'+id+'/delete';
      this.axios.post(url).then((response)=>{
        let json = response.data;
        if(json.code == 20000) {
          this.$message({
            message: '删除品牌成功！',
            type: 'success'
          });
        }else {
          this.$message.error(response.data.message);
        }
        this.loadBrands();
      })
    },
    loadBrands: function () {
      console.log('loadBrands()');
      let url = 'http://localhost:9080/brands';
      console.log('url=' + url);
      this.axios.get(url).then((response) => {
        let json = response.data;
        if (json.code == 20000) {
          // let list = json.data;
          // for (let i = 0; i < list.length; i++) {
          //   console.log(list[i]);
          // }
          this.tableData = json.data;
        } else {
          this.$message.error(response.data.message);
        }
      });
    }
  },
  created() {
    //created  >>>> 已创建,将在mounted,显示页面之前执行
    console.log('vue created');
  },
  mounted() {
    //mounted  >>> 已挂载,将在created之后,显示页面之前执行
    console.log("vue mounted");
    this.loadBrands();
  }
}
</script>

<style scoped>

</style>